package com.cyj.springbootservice.controller.authentication;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @Author C_Y_J
 * @create 2021/10/8 14:53
 **/
@RestController
@AllArgsConstructor
@RequestMapping("/cache")
public class AuthenticationController {


    private final static String wbappid = "IDAwrYz6";
    private final static String version = "1.0.0";

    private final static String getSignatureUrl = "https://miniprogram-kyc.tencentcloudapi.com/api/server/getfaceid";
    private final static String userId = "1001";

    private RestTemplate restTemplate;

    private AuthenticationBuilder authenticationBuilder;

    /**
     * 生成签名
     * <p>
     * 前置条件：请合作方确保 SIGN Ticket 已经正常获取，获取方式请参见 获取 SIGN Ticket。
     * SIGN 类型的 Ticket，其有效期为60分钟，且可多次使用。
     * 获取 token 和 SIGN Ticket 后再用 SIGN Ticket 对数据进行签名，相关代码如下所示：
     * <p>
     * [version, wbappid, SIGN Ticket , nonce, userId]
     */
    @GetMapping("/signature")
    public void getSignature() {
        String accessToken = authenticationBuilder.getAccessToken();
        String signTicket = authenticationBuilder.getSignTicket(accessToken);

        List<String> values = Arrays.asList(version, wbappid, signTicket, "kHoSxvLZGxSoFsjxlbzEoUzh5PAnTU7p", userId);
        List<String> arrList = new ArrayList<>(values);
        String sign = AuthenticationBuilder.sign(arrList);
        System.out.println(sign);
    }

    @GetMapping("/authentication")
    public void authentication() {
        String accessToken = authenticationBuilder.getAccessToken();
        String signTicket = authenticationBuilder.getSignTicket(accessToken);

        List<String> values = Arrays.asList(version, wbappid, signTicket, "kHoSxvLZGxSoFsjxlbzEoUzh5PAnTU7p", userId);
        List<String> arrList = new ArrayList<>(values);
        String sign = AuthenticationBuilder.sign(arrList);
        System.out.println(sign);

        // 请求头设置, application/json格式的数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode personJsonObject = objectMapper.createObjectNode();
        personJsonObject.put("webankAppId", wbappid);
        personJsonObject.put("name", "曹逸剑");
        personJsonObject.put("idNo", "431321199903085819");
        personJsonObject.put("orderNo", "2001");
        personJsonObject.put("userId", userId);
        personJsonObject.put("version", version);
        personJsonObject.put("sign", sign);
        personJsonObject.put("nonce", "kHoSxvLZGxSoFsjxlbzEoUzh5PAnTU7p");
        // 组装请求体
        HttpEntity<String> request = new HttpEntity<>(personJsonObject.toString(), headers);

        Object response = restTemplate.postForObject(getSignatureUrl, request, Object.class);
    }


}
